/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.koron.common.bean.StaffBean;
import com.koron.common.dfs.FastDFSClientWrapper;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.common.util.DocConverterUtils;
import com.koron.common.web.Util;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.mapper.CorpManagementMapper;
import com.koron.scene.domain.SafetyInspection;
import com.koron.scene.domain.SafetyInspectionFileBean;
import com.koron.scene.mapper.SafetyInspectionMapper;
import com.koron.scene.service.api.SafetyInspectionService;
import com.koron.scene.utils.BusinessCode;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SafetyInspectionServiceImpl implements SafetyInspectionService {


    private static final Logger logger = LoggerFactory.getLogger(SafetyInspectionServiceImpl.class);
    @Autowired
    private FastDFSClientWrapper clientWrapper;

    @Autowired
    private DocConverterUtils docConverterUtils;

    @Autowired
    private Util util;

    @Override
    public MessageBean<String> save(SafetyInspection bean, StaffBean user, MultipartFile[] files, String keepFiles, String keepPreviewFiles, String delFiles, String delPreviewFiles) {
        MessageBean<String> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), "保存成功", String.class);
        SessionFactory factory = new SessionFactory();
        List<String> keepFileList = new ArrayList<>();
        List<String> keepPreviewFileList = new ArrayList<>();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(bean.getProjectName())) {
                msg.setCode(BusinessCode.PROJECT_NAME_NOT_EMPTY.getCode());
                msg.setDescription(BusinessCode.PROJECT_NAME_NOT_EMPTY.getDescription());
                return msg;
            }
            if (bean.getInspectionDate().getTime() > bean.getReviewDate().getTime()) {
                msg.setCode(BusinessCode.REVIEW_DATE_BEFORE_INSPECT_DATE.getCode());
                msg.setDescription(BusinessCode.REVIEW_DATE_BEFORE_INSPECT_DATE.getDescription());
                return msg;
            }
            if (StringUtils.isNotEmpty(keepFiles)) {
                List<String> list = Arrays.asList(keepFiles.split("[,，;；]"));
                keepFileList.addAll(list);
            }
            if (StringUtils.isNotEmpty(keepPreviewFiles)) {
                List<String> list = Arrays.asList(keepPreviewFiles.split("[,，;；]"));
                keepPreviewFileList.addAll(list);
            }
            if (null != files && files.length > 0) {
                for (MultipartFile file : files) {
                    String url = clientWrapper.uploadFile(file);
                    //获得文件名
                    String fileName = file.getOriginalFilename();
                    keepFileList.add(url + "#" + fileName);
                    File previewFile = docConverterUtils.converter(file);
                    if (previewFile != null) {
                        String previewFilePath = clientWrapper.uploadFile(new FileInputStream(previewFile), previewFile.length(), FilenameUtils.getExtension(previewFile.getName()));
                        keepPreviewFileList.add(previewFilePath);
                    } else {
                        keepPreviewFileList.add(url);
                    }
                }
            }
            if (StringUtils.isEmpty(bean.getCode())) {
                String code = getCode(factory);
                bean.setCode(code);
            }
            if (StringUtils.isEmpty(bean.getId())) {
                bean.setId(DefaultIdGenerator.getInstance().generateLongId());
                bean.setAddTime(bean.getUpdateTime());
                bean.setAddUser(bean.getUpdateUser());
                bean.setAddUserName(bean.getUpdateUserName());
                Integer insert = mapper.insert(bean);
                if (insert <= 0) {
                    factory.close(false);
                    msg.setCode(BusinessCode.FAILURE.getCode());
                    msg.setDescription("保存失败");
                    return msg;
                } else {
                    if (StringUtils.isNotEmpty(delFiles)) {
                        String[] fileUrls = delFiles.split("[,，；;]");
                        for (String fileUrl : fileUrls) {
                            clientWrapper.deleteFile(fileUrl.split("#")[0]);
                        }
                    }
                    if (StringUtils.isNotEmpty(delPreviewFiles)) {
                        String[] fileUrls = delPreviewFiles.split("[,，；;]");
                        for (String fileUrl : fileUrls) {
                            clientWrapper.deleteFile(fileUrl.split("#")[0]);
                        }
                    }
                }
                factory.close();
                return msg;
            } else {
                SafetyInspection entity = mapper.queryOne(bean.getId());
                if (null == entity) {
                    msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                    msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                    return msg;
                }
                Integer insert = mapper.update(bean);
                if (insert <= 0) {
                    factory.close(false);
                    msg.setCode(BusinessCode.FAILURE.getCode());
                    msg.setDescription("修改失败");
                    return msg;
                } else {
                    if (StringUtils.isNotEmpty(delFiles)) {
                        String[] fileUrls = delFiles.split("[,，；;]");
                        for (String fileUrl : fileUrls) {
                            clientWrapper.deleteFile(fileUrl.split("#")[0]);
                        }
                    }
                    if (StringUtils.isNotEmpty(delPreviewFiles)) {
                        String[] fileUrls = delPreviewFiles.split("[,，；;]");
                        for (String fileUrl : fileUrls) {
                            clientWrapper.deleteFile(fileUrl.split("#")[0]);
                        }
                    }
                }
                factory.close();
                return msg;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory) {
                factory.close(false);
            }
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription("保存失败");
            return msg;
        } finally {
            msg.setData(bean.getId());
            if (factory != null) {
                factory.close();
            }
        }
    }

    private String getCode(SessionFactory factory) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StringBuilder sb = new StringBuilder();
        // 获取当前日期
        String date = sdf.format(new Date());
        StringBuilder builder = sb.append(date).append("-");
        // 根据当前时间查询已存在的最大code
        String maxCode = factory.getMapper(SafetyInspectionMapper.class).queryMaxCode(builder.toString());
        if (null == maxCode) {
            return builder.append("001").toString();
        }
        // 截取最后三位
        String substring = maxCode.substring(maxCode.length() - 3);
        //末位加1
        long lastNum = Long.parseLong("1" + substring) + 1;
        // 替换最后三位
        maxCode = maxCode.substring(0, maxCode.length() - 3) + (lastNum + "").substring(1);
        return maxCode;


    }

    @Override
    public MessageBean<Integer> delete(String id, StaffBean user) {
        MessageBean<Integer> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), "删除成功", Integer.class);
        SessionFactory factory = new SessionFactory();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            SafetyInspection inspection = mapper.queryOne(id);
            if (null == inspection) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            Integer delete = mapper.delete(id);
            if (delete <= 0) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("删除失败");
                return msg;
            }
            factory.close();
            return msg;
        } catch (Exception e) {
            if (null != factory) {
                factory.close(false);
            }
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription("删除失败");
            return msg;
        } finally {
            if (factory != null) {
                factory.close();
            }
        }
    }

    @Override
    public MessageBean<SafetyInspection> queryOne(String id) {
        MessageBean<SafetyInspection> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), "查询详情成功", SafetyInspection.class);
        SessionFactory factory = new SessionFactory();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            SafetyInspection inspection = mapper.queryOne(id);
            if (null == inspection) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            msg.setData(inspection);
            factory.close();
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory) {
                factory.close(false);
            }
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
            return msg;
        } finally {
            if (factory != null) {
                factory.close();
            }
        }
    }

    @Override
    public MessageBean<DataBean> list(String corpCode, String projectName, String startDate, String endDate, String keyword, Integer start, Integer pageSize) {
        MessageBean<DataBean> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), "查询列表成功", DataBean.class);
        SessionFactory factory = new SessionFactory();
        try {
            // 获取单位及其下属所有的子单位
            String codes = util.getDeptCodes(corpCode);
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            List<SafetyInspection> list = mapper.queryByCondition(codes, projectName, startDate, endDate, start, pageSize,keyword);
            Integer count = mapper.queryCount(codes, projectName, startDate, endDate,keyword);
            list.forEach(bean -> {
                if (StringUtils.isNotEmpty(bean.getCorpCode())) {
                    CorpBean corpBean = factory.getMapper(CorpManagementMapper.class).get(bean.getCorpCode());
                    bean.setCorpName(corpBean == null ? null : corpBean.getCorpName());
                }
            });
            DataBean<SafetyInspection> dataBean = new DataBean<>();
            dataBean.setPageSize(pageSize);
            dataBean.setList(list);
            dataBean.setTotalNumber(count);
            dataBean.setTotalPage(count > pageSize ? count % pageSize == 0 ? count / pageSize : count / pageSize + 1 : 1);
            msg.setData(dataBean);
            factory.close();
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory) {
                factory.close(false);
            }
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> saveFiles(SessionFactory factory, String id, StaffBean user, SafetyInspectionFileBean fileBean) {
        MessageBean<Integer> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Integer.class);
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            SafetyInspection inspection = mapper.queryOne(id);
            if (null == inspection) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            SetFile(fileBean);
            // 班前会
            if (null != fileBean.getKeepPSMList() && fileBean.getKeepPSMList().size() > 0) {
                inspection.setPreShiftMeetingAttachment(new Gson().toJson(fileBean.getKeepPSMList()));
            }
            if (null != fileBean.getKeepPreviewPSMList() && fileBean.getKeepPreviewPSMList().size() > 0) {
                inspection.setPreShiftMeetingPreviewAttachment(new Gson().toJson(fileBean.getKeepPreviewPSMList()));
            }
            // 安全交底书
            if (null != fileBean.getSafetyDisclosureKeepList() && fileBean.getSafetyDisclosureKeepList().size() > 0) {
                inspection.setSafetyDisclosureAttachment(new Gson().toJson(fileBean.getSafetyDisclosureKeepList()));
            }
            if (null != fileBean.getSafetyDisclosureKeepPreviewList() && fileBean.getSafetyDisclosureKeepPreviewList().size() > 0) {
                inspection.setSafetyDisclosurePreviewAttachment(new Gson().toJson(fileBean.getSafetyDisclosureKeepPreviewList()));
            }
            // 作业审批表
            if (null != fileBean.getJobFormKeepList() && fileBean.getJobFormKeepList().size() > 0) {
                inspection.setJobFormAttachment(new Gson().toJson(fileBean.getJobFormKeepList()));
            }
            if (null != fileBean.getJobFormKeepPreviewList() && fileBean.getJobFormKeepPreviewList().size() > 0) {
                inspection.setJobFormPreviewAttachment(new Gson().toJson(fileBean.getJobFormKeepPreviewList()));
            }
            // 开工报备表
            if (null != fileBean.getCommencementReportKeepList() && fileBean.getCommencementReportKeepList().size() > 0) {
                inspection.setCommencementReportAttachment(new Gson().toJson(fileBean.getCommencementReportKeepList()));
            }
            if (null != fileBean.getCommencementReportKeepPreviewList() && fileBean.getCommencementReportKeepPreviewList().size() > 0) {
                inspection.setCommencementReportPreviewAttachment(new Gson().toJson(fileBean.getCommencementReportKeepPreviewList()));
            }
            //监理日志
            if (null != fileBean.getSupervisionLogKeepList() && fileBean.getSupervisionLogKeepList().size() > 0) {
                inspection.setSupervisionLogAttachment(new Gson().toJson(fileBean.getSupervisionLogKeepList()));
            }
            if (null != fileBean.getSupervisionLogKeepPreviewList() && fileBean.getSupervisionLogKeepPreviewList().size() > 0) {
                inspection.setSupervisionLogPreviewAttachment(new Gson().toJson(fileBean.getSupervisionLogKeepPreviewList()));
            }
            //施工日志
            if (null != fileBean.getConstructionLogKeepList() && fileBean.getConstructionLogKeepList().size() > 0) {
                inspection.setConstructionLogAttachment(new Gson().toJson(fileBean.getConstructionLogKeepList()));
            }
            if (null != fileBean.getConstructionLogKeepPreviewList() && fileBean.getConstructionLogKeepPreviewList().size() > 0) {
                inspection.setConstructionLogPreviewAttachment(new Gson().toJson(fileBean.getConstructionLogKeepPreviewList()));
            }
            // 其他
            if (null != fileBean.getOtherKeepList() && fileBean.getOtherKeepList().size() > 0) {
                inspection.setOtherAttachment(new Gson().toJson(fileBean.getOtherKeepList()));
            }
            if (null != fileBean.getOtherKeepPreviewList() && fileBean.getOtherKeepPreviewList().size() > 0) {
                inspection.setOtherPreviewAttachment(new Gson().toJson(fileBean.getOtherKeepPreviewList()));
            }
            Integer update = mapper.update(inspection);
            if (update > 0) {
                deleteFile(fileBean);
            }
            return msg;
        } catch (Exception e) {
            deleteKeepFile(fileBean);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FILE_IS_EMPTY.getCode());
            msg.setDescription("保存附件失败");
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> saveFile(SessionFactory factory, String id, StaffBean user, Integer type, MultipartFile[] files) {
        MessageBean<Integer> msg = new MessageBean<>();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            if (null == files || files.length == 0) {
                msg.setCode(BusinessCode.CHECKRECORD_EMPTY_CHANGE_FILE.getCode());
                msg.setDescription(BusinessCode.CHECKRECORD_EMPTY_CHANGE_FILE.getDescription());
                return msg;
            }
            SafetyInspection entity = mapper.queryOne(id);
            if (null == entity) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            List<String> atts = new ArrayList<>();
            List<String> preAtts = new ArrayList<>();
            switch (type) {
                case 1:
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingAttachment())) {
                        atts = JSONObject.parseArray(entity.getPreShiftMeetingAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getPreShiftMeetingPreviewAttachment(), String.class);
                    }
                    break;
                case 2:
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosureAttachment())) {
                        atts = JSONObject.parseArray(entity.getSafetyDisclosureAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosurePreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSafetyDisclosurePreviewAttachment(), String.class);
                    }
                    break;
                case 3:
                    if (StringUtils.isNotEmpty(entity.getJobFormAttachment())) {
                        atts = JSONObject.parseArray(entity.getJobFormAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getJobFormPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getJobFormPreviewAttachment(), String.class);
                    }
                    break;
                case 4:
                    if (StringUtils.isNotEmpty(entity.getCommencementReportAttachment())) {
                        atts = JSONObject.parseArray(entity.getCommencementReportAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getCommencementReportPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getCommencementReportPreviewAttachment(), String.class);
                    }
                    break;
                case 5:
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getSupervisionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSupervisionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 6:
                    if (StringUtils.isNotEmpty(entity.getConstructionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getConstructionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getConstructionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getConstructionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 7:
                    if (StringUtils.isNotEmpty(entity.getOtherAttachment())) {
                        atts = JSONObject.parseArray(entity.getOtherAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getOtherPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getOtherPreviewAttachment(), String.class);
                    }
                    break;
            }
            for (MultipartFile file : files) {
                String url = clientWrapper.uploadFile(file);
                atts.add(url + "#" + file.getOriginalFilename());
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    preAtts.add(previewFilePath);
                } else {
                    preAtts.add(url);
                }
            }
            switch (type) {
                case 1:
                    entity.setPreShiftMeetingAttachment(new Gson().toJson(atts));
                    entity.setPreShiftMeetingPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 2:
                    entity.setSafetyDisclosureAttachment(new Gson().toJson(atts));
                    entity.setSafetyDisclosurePreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 3:
                    entity.setJobFormAttachment(new Gson().toJson(atts));
                    entity.setJobFormPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 4:
                    entity.setCommencementReportAttachment(new Gson().toJson(atts));
                    entity.setCommencementReportPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 5:
                    entity.setSupervisionLogAttachment(new Gson().toJson(atts));
                    entity.setSupervisionLogPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 6:
                    entity.setConstructionLogAttachment(new Gson().toJson(atts));
                    entity.setConstructionLogPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 7:
                    entity.setOtherAttachment(new Gson().toJson(atts));
                    entity.setOtherPreviewAttachment(new Gson().toJson(preAtts));
                    break;
            }
            Integer value = mapper.update(entity);
            if (value > 0) {
                factory.close();
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("上传成功");
                return msg;
            } else {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("上传失败");
                return msg;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            factory.close(false);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Integer> delFile(SessionFactory factory, String id, StaffBean user, Integer type, String fileUrl, String filePreUrl) {
        MessageBean<Integer> msg = new MessageBean<>();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            SafetyInspection entity = mapper.queryOne(id);
            if (null == entity) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            List<String> atts = new ArrayList<>();
            List<String> preAtts = new ArrayList<>();
            switch (type) {
                case 1:
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingAttachment())) {
                        atts = JSONObject.parseArray(entity.getPreShiftMeetingAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getPreShiftMeetingPreviewAttachment(), String.class);
                    }
                    break;
                case 2:
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosureAttachment())) {
                        atts = JSONObject.parseArray(entity.getSafetyDisclosureAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosurePreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSafetyDisclosurePreviewAttachment(), String.class);
                    }
                    break;
                case 3:
                    if (StringUtils.isNotEmpty(entity.getJobFormAttachment())) {
                        atts = JSONObject.parseArray(entity.getJobFormAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getJobFormPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getJobFormPreviewAttachment(), String.class);
                    }
                    break;
                case 4:
                    if (StringUtils.isNotEmpty(entity.getCommencementReportAttachment())) {
                        atts = JSONObject.parseArray(entity.getCommencementReportAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getCommencementReportPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getCommencementReportPreviewAttachment(), String.class);
                    }
                    break;
                case 5:
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getSupervisionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSupervisionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 6:
                    if (StringUtils.isNotEmpty(entity.getConstructionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getConstructionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getConstructionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getConstructionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 7:
                    if (StringUtils.isNotEmpty(entity.getOtherAttachment())) {
                        atts = JSONObject.parseArray(entity.getOtherAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getOtherPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getOtherPreviewAttachment(), String.class);
                    }
                    break;
            }
            if (atts.contains(fileUrl)) {
                //删除文件
                atts.remove(fileUrl);
            }
            if (preAtts.contains(filePreUrl)) {
                //删除文件
                preAtts.remove(filePreUrl);
            }
            switch (type) {
                case 1:
                    entity.setPreShiftMeetingAttachment(new Gson().toJson(atts));
                    entity.setPreShiftMeetingPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 2:
                    entity.setSafetyDisclosureAttachment(new Gson().toJson(atts));
                    entity.setSafetyDisclosurePreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 3:
                    entity.setJobFormAttachment(new Gson().toJson(atts));
                    entity.setJobFormPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 4:
                    entity.setCommencementReportAttachment(new Gson().toJson(atts));
                    entity.setCommencementReportPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 5:
                    entity.setSupervisionLogAttachment(new Gson().toJson(atts));
                    entity.setSupervisionLogPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 6:
                    entity.setConstructionLogAttachment(new Gson().toJson(atts));
                    entity.setConstructionLogPreviewAttachment(new Gson().toJson(preAtts));
                    break;
                case 7:
                    entity.setOtherAttachment(new Gson().toJson(atts));
                    entity.setOtherPreviewAttachment(new Gson().toJson(preAtts));
                    break;
            }
            Integer value = mapper.update(entity);
            if (value > 0) {
                clientWrapper.deleteFile(fileUrl.split("#")[0]);
                clientWrapper.deleteFile(filePreUrl.split("#")[0]);
                factory.close();
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription("操作成功");
                return msg;
            } else {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("操作失败");
                return msg;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<Map> fileList(SessionFactory factory, String id, StaffBean user, Integer type) {
        MessageBean<Map> msg = new MessageBean<>();
        try {
            SafetyInspectionMapper mapper = factory.getMapper(SafetyInspectionMapper.class);
            if (StringUtils.isEmpty(id)) {
                msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
                msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
                return msg;
            }
            SafetyInspection entity = mapper.queryOne(id);
            if (null == entity) {
                msg.setCode(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getCode());
                msg.setDescription(BusinessCode.SAFETY_INSPECTION_NOT_EXIST.getDescription());
                return msg;
            }
            List<String> atts = new ArrayList<>();
            List<String> preAtts = new ArrayList<>();
            switch (type) {
                case 1:
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingAttachment())) {
                        atts = JSONObject.parseArray(entity.getPreShiftMeetingAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getPreShiftMeetingPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getPreShiftMeetingPreviewAttachment(), String.class);
                    }
                    break;
                case 2:
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosureAttachment())) {
                        atts = JSONObject.parseArray(entity.getSafetyDisclosureAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSafetyDisclosurePreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSafetyDisclosurePreviewAttachment(), String.class);
                    }
                    break;
                case 3:
                    if (StringUtils.isNotEmpty(entity.getJobFormAttachment())) {
                        atts = JSONObject.parseArray(entity.getJobFormAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getJobFormPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getJobFormPreviewAttachment(), String.class);
                    }
                    break;
                case 4:
                    if (StringUtils.isNotEmpty(entity.getCommencementReportAttachment())) {
                        atts = JSONObject.parseArray(entity.getCommencementReportAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getCommencementReportPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getCommencementReportPreviewAttachment(), String.class);
                    }
                    break;
                case 5:
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getSupervisionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getSupervisionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getSupervisionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 6:
                    if (StringUtils.isNotEmpty(entity.getConstructionLogAttachment())) {
                        atts = JSONObject.parseArray(entity.getConstructionLogAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getConstructionLogPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getConstructionLogPreviewAttachment(), String.class);
                    }
                    break;
                case 7:
                    if (StringUtils.isNotEmpty(entity.getOtherAttachment())) {
                        atts = JSONObject.parseArray(entity.getOtherAttachment(), String.class);
                    }
                    if (StringUtils.isNotEmpty(entity.getOtherPreviewAttachment())) {
                        preAtts = JSONObject.parseArray(entity.getOtherPreviewAttachment(), String.class);
                    }
                    break;
            }
            Map map = new HashMap();
            map.put("url", new Gson().toJson(atts));
            map.put("preUrl", new Gson().toJson(preAtts));
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription("获取附件列表成功");
            msg.setData(map);
            factory.close();
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }


    /**
     * 整理保存的文件
     *
     * @param fileBean
     * @throws Exception
     */
    public void SetFile(SafetyInspectionFileBean fileBean) throws Exception {
        // 班前会
        MultipartFile[] preShiftMeetingFiles = fileBean.getPreShiftMeetingFiles();
        if (null != preShiftMeetingFiles && preShiftMeetingFiles.length > 0) {
            for (MultipartFile file : preShiftMeetingFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getKeepPSMList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getKeepPreviewPSMList().add(previewFilePath);
                } else {
                    fileBean.getKeepPreviewPSMList().add(url);
                }
            }
        }
        String keepPSMFiles = fileBean.getPreShiftMeetingKeepFiles();
        String keepPreviewPSMFiles = fileBean.getPreShiftMeetingKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepPSMFiles)) {
            String[] keepArray = keepPSMFiles.split("[,，]");
            fileBean.getKeepPSMList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewPSMFiles)) {
            String[] keepArray = keepPreviewPSMFiles.split("[,，]");
            fileBean.getKeepPreviewPSMList().addAll(Arrays.asList(keepArray));
        }
        // 安全交底书
        MultipartFile[] safetyDisclosureFiles = fileBean.getSafetyDisclosureFiles();
        if (null != safetyDisclosureFiles && safetyDisclosureFiles.length > 0) {
            for (MultipartFile file : safetyDisclosureFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getSafetyDisclosureKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getSafetyDisclosureKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getSafetyDisclosureKeepPreviewList().add(url);
                }
            }
        }
        String sdFiles = fileBean.getSafetyDisclosureKeepFiles();
        String previewSDFiles = fileBean.getSafetyDisclosureKeepPreviewFiles();
        if (StringUtils.isNotEmpty(sdFiles)) {
            String[] keepArray = sdFiles.split("[,，]");
            fileBean.getSafetyDisclosureKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(previewSDFiles)) {
            String[] keepArray = previewSDFiles.split("[,，]");
            fileBean.getSafetyDisclosureKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
        // 作业审批表
        MultipartFile[] jobFormFiles = fileBean.getJobFormFiles();
        if (null != jobFormFiles && jobFormFiles.length > 0) {
            for (MultipartFile file : jobFormFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getJobFormKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getJobFormKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getJobFormKeepPreviewList().add(url);
                }
            }
        }
        String keepJobFormFiles = fileBean.getJobFormKeepFiles();
        String keepPreviewJobFormFiles = fileBean.getJobFormKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepJobFormFiles)) {
            String[] keepArray = keepJobFormFiles.split("[,，]");
            fileBean.getJobFormKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewJobFormFiles)) {
            String[] keepArray = keepPreviewJobFormFiles.split("[,，]");
            fileBean.getJobFormKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
        // 开工报备表
        MultipartFile[] commencementReportFiles = fileBean.getCommencementReportFiles();
        if (null != commencementReportFiles && commencementReportFiles.length > 0) {
            for (MultipartFile file : commencementReportFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getCommencementReportKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getCommencementReportKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getCommencementReportKeepPreviewList().add(url);
                }
            }
        }
        String keepKGBBFiles = fileBean.getCommencementReportKeepFiles();
        String keepPreviewKGBBFiles = fileBean.getCommencementReportKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepKGBBFiles)) {
            String[] keepArray = keepKGBBFiles.split("[,，]");
            fileBean.getCommencementReportKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewKGBBFiles)) {
            String[] keepArray = keepPreviewKGBBFiles.split("[,，]");
            fileBean.getCommencementReportKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
        // 监理日志
        MultipartFile[] supervisionLogFiles = fileBean.getSupervisionLogFiles();
        if (null != supervisionLogFiles && supervisionLogFiles.length > 0) {
            for (MultipartFile file : supervisionLogFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getSupervisionLogKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getSupervisionLogKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getSupervisionLogKeepPreviewList().add(url);
                }
            }
        }
        String keepJLRZFiles = fileBean.getSupervisionLogKeepFiles();
        String keepPreviewJLRZFiles = fileBean.getSupervisionLogKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepJLRZFiles)) {
            String[] keepArray = keepJLRZFiles.split("[,，]");
            fileBean.getSupervisionLogKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewJLRZFiles)) {
            String[] keepArray = keepPreviewJLRZFiles.split("[,，]");
            fileBean.getSupervisionLogKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
        // 施工日志
        MultipartFile[] constructionLogFiles = fileBean.getConstructionLogFiles();
        if (null != constructionLogFiles && constructionLogFiles.length > 0) {
            for (MultipartFile file : constructionLogFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getConstructionLogKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getConstructionLogKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getConstructionLogKeepPreviewList().add(url);
                }
            }
        }
        String keepSGRZFiles = fileBean.getConstructionLogKeepFiles();
        String keepPreviewSGRZFiles = fileBean.getConstructionLogKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepSGRZFiles)) {
            String[] keepArray = keepSGRZFiles.split("[,，]");
            fileBean.getConstructionLogKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewSGRZFiles)) {
            String[] keepArray = keepPreviewSGRZFiles.split("[,，]");
            fileBean.getConstructionLogKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
        // 其他
        MultipartFile[] otherFiles = fileBean.getOtherFiles();
        if (null != otherFiles && otherFiles.length > 0) {
            for (MultipartFile file : otherFiles) {
                String url = clientWrapper.uploadFile(file);
                //获得文件名
                String fileName = file.getOriginalFilename();
                fileBean.getOtherKeepList().add(url + "#" + fileName);
                String previewFilePath = null;
                try {
                    previewFilePath = docConverterUtils.converterUpdate(file);
                } catch (Exception e) {
                    logger.error(e.getMessage(),e);
                }
                if (StringUtils.isNotEmpty(previewFilePath)) {
                    fileBean.getOtherKeepPreviewList().add(previewFilePath);
                } else {
                    fileBean.getOtherKeepPreviewList().add(url);
                }
            }
        }
        String keepOtherFiles = fileBean.getOtherKeepFiles();
        String keepPreviewOtherFiles = fileBean.getOtherKeepPreviewFiles();
        if (StringUtils.isNotEmpty(keepOtherFiles)) {
            String[] keepArray = keepOtherFiles.split("[,，]");
            fileBean.getOtherKeepList().addAll(Arrays.asList(keepArray));
        }
        if (StringUtils.isNotEmpty(keepPreviewOtherFiles)) {
            String[] keepArray = keepPreviewOtherFiles.split("[,，]");
            fileBean.getOtherKeepPreviewList().addAll(Arrays.asList(keepArray));
        }
    }

    /**
     * 删除文件
     *
     * @param fileBean
     */
    private void deleteKeepFile(SafetyInspectionFileBean fileBean) {
        // 班前会
        List<String> keepPSMList = fileBean.getKeepPSMList();
        if (null != keepPSMList && keepPSMList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getPreShiftMeetingKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getPreShiftMeetingKeepFiles().split("[,，]"));
                for (String url : keepPSMList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPSMList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewPSMList = fileBean.getKeepPreviewPSMList();
        if (null != keepPreviewPSMList && keepPreviewPSMList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getPreShiftMeetingDelFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getPreShiftMeetingDelFiles().split("[,，]"));
                for (String url : keepPreviewPSMList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewPSMList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 安全交底书
        List<String> keepSDList = fileBean.getSafetyDisclosureKeepList();
        if (null != keepSDList && keepSDList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getSafetyDisclosureKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getSafetyDisclosureKeepFiles().split("[,，]"));
                for (String url : keepSDList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepSDList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewSDList = fileBean.getSafetyDisclosureKeepPreviewList();
        if (null != keepPreviewSDList && keepPreviewSDList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getSafetyDisclosureKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getSafetyDisclosureKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewSDList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewSDList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 作业审批表
        List<String> keepJobFormList = fileBean.getJobFormKeepList();
        if (null != keepJobFormList && keepJobFormList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getJobFormKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getJobFormKeepFiles().split("[,，]"));
                for (String url : keepJobFormList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepJobFormList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewJobFormList = fileBean.getJobFormKeepPreviewList();
        if (null != keepPreviewJobFormList && keepPreviewJobFormList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getJobFormKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getJobFormKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewJobFormList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewJobFormList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 开工报备表
        List<String> keepKGBBList = fileBean.getCommencementReportKeepList();
        if (null != keepKGBBList && keepKGBBList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getCommencementReportKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getCommencementReportKeepFiles().split("[,，]"));
                for (String url : keepKGBBList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepKGBBList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewKGBBList = fileBean.getCommencementReportKeepPreviewList();
        if (null != keepPreviewKGBBList && keepPreviewKGBBList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getCommencementReportKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getCommencementReportKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewKGBBList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewKGBBList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 监理日志
        List<String> keepJLRZList = fileBean.getSupervisionLogKeepList();
        if (null != keepJLRZList && keepJLRZList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getSupervisionLogKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getSupervisionLogKeepFiles().split("[,，]"));
                for (String url : keepJLRZList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepJLRZList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewJLRZList = fileBean.getSupervisionLogKeepPreviewList();
        if (null != keepPreviewJLRZList && keepPreviewJLRZList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getSupervisionLogKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getSupervisionLogKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewJLRZList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewJLRZList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 施工日志
        List<String> keepSGRZList = fileBean.getConstructionLogKeepList();
        if (null != keepSGRZList && keepSGRZList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getConstructionLogKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getConstructionLogKeepFiles().split("[,，]"));
                for (String url : keepSGRZList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepSGRZList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewSGRZList = fileBean.getConstructionLogKeepPreviewList();
        if (null != keepPreviewSGRZList && keepPreviewSGRZList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getConstructionLogKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getConstructionLogKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewSGRZList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewSGRZList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        // 其他
        List<String> keepOtherList = fileBean.getOtherKeepList();
        if (null != keepOtherList && keepOtherList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getOtherKeepFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getOtherKeepFiles().split("[,，]"));
                for (String url : keepOtherList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepOtherList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
        List<String> keepPreviewOtherList = fileBean.getOtherKeepPreviewList();
        if (null != keepPreviewOtherList && keepPreviewOtherList.size() > 0) {
            if (StringUtils.isNotEmpty(fileBean.getOtherKeepPreviewFiles())) {
                List<String> keeps = Arrays.asList(fileBean.getOtherKeepPreviewFiles().split("[,，]"));
                for (String url : keepPreviewOtherList) {
                    if (!keeps.contains(url)) {
                        clientWrapper.deleteFile(url.split("#")[0]);
                    }
                }
            } else {
                for (String url : keepPreviewOtherList) {
                    clientWrapper.deleteFile(url.split("#")[0]);
                }
            }
        }
    }

    private void deleteFile(SafetyInspectionFileBean fileBean) {
        //班前会
        deleteFile(fileBean.getPreShiftMeetingDelFiles());
        deleteFile(fileBean.getPreShiftMeetingDelPreviewFiles());
        // 安全交底书
        deleteFile(fileBean.getSafetyDisclosureDelFiles());
        deleteFile(fileBean.getSafetyDisclosureDelPreviewFiles());
        //作业审批表
        deleteFile(fileBean.getJobFormDelFiles());
        deleteFile(fileBean.getJobFormDelPreviewFiles());
        //开工报备表
        deleteFile(fileBean.getCommencementReportDelFiles());
        deleteFile(fileBean.getCommencementReportDelPreviewFiles());
        //监理日志
        deleteFile(fileBean.getSupervisionLogDelFiles());
        deleteFile(fileBean.getSupervisionLogDelPreviewFiles());
        //施工日志
        deleteFile(fileBean.getConstructionLogDelFiles());
        deleteFile(fileBean.getConstructionLogDelPreviewFiles());
        //其他
        deleteFile(fileBean.getOtherDelFiles());
        deleteFile(fileBean.getOtherDelPreviewFiles());
    }

    public void deleteFile(String delFiles) {
        if (StringUtils.isNotEmpty(delFiles)) {
            String[] delArray = delFiles.split("[,，]");
            for (String delUrl : delArray) {
                clientWrapper.deleteFile(delUrl.split("#")[0]);
            }
        }
    }
}
